# -*- encoding=utf8 -*-
__author__ = "Bryan.Chen"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

using("../FlowTest.air")
import FlowTest
import random

class RedMatch_RedPacketMatch(FlowTest.BaseTestCase):

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def runTest(self, args):
        
        try:
            poco("tablescene").wait_for_appearance()
        except:
            print("比赛等待界面打开超时")

        assert_equal(poco("tablescene").exists(), True, "打开房间界面正常")
        
        gameLayer = poco("tablescene").child("gamelayer")

        if not gameLayer.exists():
            return True 
        
        while True:
        
            calllordLayer = gameLayer.child("calllordlayer")
            cardopt = gameLayer.child("cardopt")
            handcardLayer = gameLayer.offspring("handcardlayer")
            
            if calllordLayer.exists() and calllordLayer.attr("visible"):
                # 叫抢地主
                try: 
                    n = random.randint(1, 10)
                    if n % 2 == 0:
                        btnCall = calllordLayer.offspring("btn_call")
                        if btnCall and btnCall.attr("visible"):
                            self.clickButton(btnCall)
                    else: 
                        btnNoCall = calllordLayer.offspring("btn_nocall")
                        if btnNoCall and btnCall.attr("visible"):
                            self.clickButton(btnNoCall)
                except:
                    print("")
                    
            if cardopt.exists():
                # 首出，出最小一张牌
                optCardLayer = cardopt.offspring("opt_card_layer")
                
                isDone = False
                try: 
                    if not isDone:
                        btnPass = cardopt.offspring("notout_btn")
                        btnRemind = cardopt.offspring("remind_btn")
                        btnOutCard = cardopt.offspring("out_btn")

                        if btnPass.attr("visible") and btnRemind.attr("visible") and btnOutCard.attr("visible"):
                            self.clickButton(btnRemind)
                            self.clickButton(btnOutCard)

                        isDone = True
                except:
                    print("")
                    
                try:
                    if not isDone:
                        btnOutCard = cardopt.offspring("out_btn")
                        if btnOutCard.attr("visible"):
                            allCards = handcardLayer.offspring("<Node | Tag = -1")
                            allCards[len(allCards) - 1].click()
                            self.clickButton(btnOutCard)

                        isDone = True
                except:
                    print("")
                    
                try:
                    if not isDone:
                        btnUnable = cardopt.offspring("unable_btn")
                        if btnUnable.attr("visible"):
                            self.clickButton(btnUnable)

                        isDone = True
                except:
                    print("")

            passMatchLayer = poco("PassMatchLayer")
            if passMatchLayer.exists():
                # 进入晋级界面
                # 等待，什么都不做
                print("")
        
        
            settleMatchLayer = poco("SettleMatchLayer")
            if settleMatchLayer.exists():
                # 进入奖励界面
                specialLayer = settleMatchLayer.offspring("Panel_special")
                if specialLayer.exists():
                    txtSpecialName = specialLayer.offspring("Text_specialName")
                    # assert_equal(True, True, "获得" % txtSpecialName )
                    
                    sleep(1.0)
                    btnSpecialBack = specialLayer.offspring("Button_specialBack")
                    self.clickButton(btnSpecialBack)
                    break
                
                settlePanel = settleMatchLayer.offspring("Panel_settle")
                if settlePanel.exists():
                    txtMatchName = settlePanel.offspring("Text_matchName").get_text()
                    txtRank = settlePanel.offspring("AtlasLabel_rank").get_text()
                    # assert_equal(True, True, ("%s第%名" % (txtMatchName, txtRank)))

                    sleep(1.0)
                    btnBack = settlePanel.offspring("Button_back")
                    self.clickButton(btnBack) # 点击返回按钮，返回到大厅
                    break

        btnBack = poco("SelectMatchLayer").offspring("Button_back")
        if btnBack.exists():
            self.clickButton(btnBack)

        return True
        
_instance = RedMatch_RedPacketMatch()
runTest = _instance.runTest

